package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;


    @Autowired
    private IStatementService statementService;


    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        //1.合理性的效验
        //只有处于预约中的状态才可以进行编辑操作
        Appointment oldAppointment = this.get(appointment.getId());
        if(!Appointment.STATUS_APPOINTMENT.equals(oldAppointment.getStatus())){
           //非预约中，不能进行编辑
            throw new BusinessException("非预约中，不能进行编辑！");
        }
        //xml中处理特殊（前端无传的）字段
        appointmentMapper.updateByPrimaryKey(appointment);

    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            appointmentMapper.deleteByPrimaryKey(dictId);

        }
    }




    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void cancel(Long id) {
        //1.合理性效验
        //只有预约中才可以进行取消
        Appointment oldAppointment = this.get(id);
        if(!Appointment.STATUS_APPOINTMENT.equals(oldAppointment.getStatus())){
            throw new BusinessException("非预约中，不能进行取消！");
        }
        appointmentMapper.changStatus(id,Appointment.STATUS_CANCEL);
    }

    @Override
    public void deleteById(Long id) {
        //1.合理性效验
        //只有预约中才可以进行删除
        Appointment oldAppointment = this.get(id);
        if(!Appointment.STATUS_APPOINTMENT.equals(oldAppointment.getStatus())){
            throw new BusinessException("非预约中，不能进行删除！");
        }
        appointmentMapper.updateById(id);
    }

    @Override
    public void arrival(Long id) {
       //1.合理性效验
        //只有预约中才可以进行到店处理
        Appointment oldAppointment = this.get(id);
        if(!Appointment.STATUS_APPOINTMENT.equals(oldAppointment.getStatus())){
            throw new BusinessException("非预约中，不能进行到店处理！");
        }
        appointmentMapper.arrivalById(id,Appointment.STATUS_ARRIVAL,new Date());

    }


    @Override
    public Statement generateStatement(Long appointmentId) {
        //获取预约单对象
        Appointment appointment = this.get(appointmentId);
        //合理性效验
        if(!(Appointment.STATUS_ARRIVAL.equals(appointment.getStatus()) ||Appointment.STATUS_SETTLE.equals(appointment.getStatus()))){
            //非(已到店或已结算）
            throw new BusinessException("只有处于已到店和已结算状态才能进行结算单处理");
        }

        Statement statement = statementService.getByAppointmentId(appointmentId);
        if(statement == null){
            //没有找到记录，说明此预约单没有生成结算记录
            statement = new Statement();
            statement.setAppointmentId(appointmentId); //关联预约单ID
            statement.setCustomerName(appointment.getCustomerName());//客户名称
            statement.setCustomerPhone(appointment.getCustomerPhone()); //客户手机
            statement.setActualArrivalTime(appointment.getActualArrivalTime());//到店时间
            statement.setLicensePlate(appointment.getLicensePlate());//车牌号码
            statement.setCarSeries(appointment.getCarSeries());//汽车类型
            statement.setServiceType(appointment.getServiceType()); // 服务类型
            statement.setCreateTime(new Date()); //记录创建时间
            statement.setInfo(appointment.getInfo()); // 备注信息
            //保存结算单
            statementService.save(statement);
        }

        return statement;
    }

    @Override
    public void updateStatus(Long appointmentId, Integer status) {
        appointmentMapper.changStatus(appointmentId,status);
    }
}
